print('no error here 7')
## [1] "no error here 7"
library(sf)
## Linking to GEOS 3.8.1, GDAL 3.1.1, PROJ 6.3.1
print('no error here 8')
## [1] "no error here 8"
library(tidyverse)
## ── Attaching packages ─────────────────────────────────────────────────────────────────────── tidyverse 1.3.0 ──
## ✓ ggplot2 3.3.2     ✓ purrr   0.3.4
## ✓ tibble  3.0.3     ✓ dplyr   1.0.1
## ✓ tidyr   1.1.1     ✓ stringr 1.4.0
## ✓ readr   1.3.1     ✓ forcats 0.5.0
## ── Conflicts ────────────────────────────────────────────────────────────────────────── tidyverse_conflicts() ──
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
print('no error here 9')
## [1] "no error here 9"
library(USAboundaries)
print('no error here 10')
## [1] "no error here 10"
library(ggthemes)
print('no error here 11')
## [1] "no error here 11"
library(units)
## udunits system database from /Library/Frameworks/R.framework/Versions/4.0/Resources/library/units/share/udunits
print('no error here 12')
## [1] "no error here 12"
library(plotly)
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
#Download file, unzip, and read in fire data
# temp <- tempfile()
# url <- 'https://firms.modaps.eosdis.nasa.gov/data/active_fire/suomi-npp-viirs-c2/shapes/zips/SUOMI_VIIRS_C2_Global_24h.zip'
# download.file(url,temp)
# unzip(temp, exdir = 'data')
print('no error here 13')
## [1] "no error here 13"
 data <- read_sf('data/SUOMI_VIIRS_C2_Global_24h.shp', crs = 4326) %>% st_transform(5070)
# unlink(temp)

#data <- read_sf('/Users/matthewcoleman/Documents/GitHub/wildfiremap/data/SUOMI_VIIRS_C2_Global_24h.shp', crs = 4326) %>% st_transform(5070)
#get the county data, filter to CA and add area column for point area.
print('no error here 14')
## [1] "no error here 14"
counties_raw <- USAboundaries::us_counties(resolution = 'low') %>%  
  st_transform(crs = 4326) %>% st_transform(5070)

print('no error here 15')
## [1] "no error here 15"
counties_ca <- counties_raw %>% filter(state_name == 'California') #%>% st_combine() 

print('no error here 16')
## [1] "no error here 16"
california <- counties_ca %>% st_combine() 

print('no error here 17')
## [1] "no error here 17"
ca_fires <- st_filter(data, california, .predicate = st_within) %>% 
  st_as_sf() %>% 
  mutate(area_km = (375^2)/1000000)

print('no error here 18')
## [1] "no error here 18"
county_indices <- st_within(ca_fires, counties_ca)

print('no error here 19')
## [1] "no error here 19"
ca_fires <- ca_fires %>% mutate(county = counties_ca$name[unlist(county_indices)]) %>% 
  st_buffer(375)
point_in_polygon = function(points, polygon, id){
  st_join(points, polygon) %>%
    st_drop_geometry() %>%
    count(.data[[id]]) %>%
    setNames(c(id, "n")) %>%
    left_join(polygon, by = id) %>%
    st_as_sf()
}

fire_counts <- point_in_polygon(ca_fires, counties_ca, 'countyfp')
#Plot Map

g <- ggplot() +
  geom_sf(data = california) +
  geom_sf(data = fire_counts, aes(fill = n), alpha = .9) +
  geom_sf(data = ca_fires, col = 'red4', alpha = .5, size = .5) +
  scale_fill_gradient(low = "white", high = "orange", name ='Fire Pixel \n Count' ) +
  theme_linedraw() +
  labs(x = 'Longitude',
       y = 'Latitude', 
       title = paste0('Location of Fires in California on ', format(Sys.Date(), format = '%b %d, %Y')) )

ggplotly(g)
fire_counts <- ca_fires %>% 
  group_by(county) %>% 
  summarise(fire_count = n()) %>% #, fire_area = sum(area_km)) %>% 
  ungroup() %>% 
  st_set_geometry(NULL)
## `summarise()` ungrouping output (override with `.groups` argument)

Code GitHub Data